package schudel.daniel.androidspellingquiz;


import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity implements OnTouchListener, OnInitListener {
    private AudioManager audio;
    private static final String TAG = "QuizActivity: ";
    private TextToSpeech myTTS = null;
    private ProgressBar progressBarWordProgress;
    private SpellingQuizApplication app;
    private TextView textViewWordProgress;
    private EditText editTextWordEntry;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        
        setContentView(R.layout.activity_quiz);
        
        audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    	myTTS = new TextToSpeech(this, this);
        progressBarWordProgress = (ProgressBar)findViewById(R.id.progressBarWordProgress);
        app = (SpellingQuizApplication) this.getApplication();
        textViewWordProgress = (TextView)findViewById(R.id.textViewWordProgress);
        editTextWordEntry = (EditText)findViewById(R.id.editTextWordEntry);
    }

    protected void onStart() {
    	super.onStart();
        Log.d(TAG, "onStart");
 
        setTitle("Testing List \"" + app.getCurrentListName() + "\"");
        progressBarWordProgress.setMax(app.wordsInCurrentQuiz());

        setProgress();
    }
    
    protected void onRestart() {
        Log.d(TAG, "onRestart");
    	super.onRestart();
    }

    protected void onResume() {
        Log.d(TAG, "onResume");
    	super.onResume();
        sayCurrentWord();
    }

    protected void onPause() {
        Log.d(TAG, "onPause");
    	super.onPause();
    }

    protected void onStop() {
        Log.d(TAG, "onStop");
    	super.onStop();
    }

    protected void onDestroy() {
        Log.d(TAG, "onDestroy");

        //Close the Text to Speech Library
        if(myTTS != null) {
            myTTS.stop();
            myTTS.shutdown();
            Log.d(TAG, "TTS Destroyed");
        }

    	super.onDestroy();
    }

    // why remove @Override ?
    public void onInit(int status)
    {
	if (status == TextToSpeech.SUCCESS)
	{
	    Locale defaultOrPassedIn = Locale.getDefault();
	   
	    // check if language is available
	    switch (myTTS.isLanguageAvailable(defaultOrPassedIn))
	    {
		case TextToSpeech.LANG_AVAILABLE:
		case TextToSpeech.LANG_COUNTRY_AVAILABLE:
		case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
		    Log.d(TAG, "SUPPORTED");
		    myTTS.setLanguage(defaultOrPassedIn);
		    sayCurrentWord();
		    //pass the tts back to the main
		    //activity for use
		    break;
		case TextToSpeech.LANG_MISSING_DATA:
		    Log.d(TAG, "MISSING_DATA");
			Log.d(TAG, "require data...");
			Intent installIntent = new Intent();
			installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
			startActivity(installIntent);
		    break;
		case TextToSpeech.LANG_NOT_SUPPORTED:
		    Log.d(TAG, "NOT SUPPORTED");
		    break;
	    }
	}
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
            return true;
        default:
            return(super.onKeyDown(keyCode, event));
        }
    }

    public boolean onTouch(View view, MotionEvent me) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    private void setProgress() {
        int total_words = app.wordsInCurrentQuiz();
        int current_cnt = app.indexInCurrentQuiz();
        progressBarWordProgress.setProgress(current_cnt);
        current_cnt+=1;
        textViewWordProgress.setText("Word " + current_cnt + " out of " + total_words);
    }

    private void sayCurrentWord() {
    	String current = app.getCurrentWord();
    	
        //speak straight away
        if (myTTS != null) {
            myTTS.speak(current, TextToSpeech.QUEUE_FLUSH, null);
        } else {
            Context context = getApplicationContext();
            Toast toast = Toast.makeText(context, current, Toast.LENGTH_SHORT);
            toast.show();
       }
    }
    
    public void buttonOnClickNextWord(View view) {
    	// get value of text entry and submit it
        String answer = editTextWordEntry.getText().toString();
        editTextWordEntry.setText("");
        if (false == app.addGuess(answer)) {
        	// move to next word
        	sayCurrentWord();
        	setProgress();
        } else {
            // move to next activity that checks answers
            Intent intent = new Intent(this, ResultsActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }
    }     
    
    public void buttonOnClickSayWord(View view) {
    	sayCurrentWord();
    }     
}
